﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Reflection;
using DevExpress.XtraSplashScreen;

namespace Baubixinh_client
{
    public partial class frmMain : DevExpress.XtraEditors.XtraForm
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void mniImportList_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                SplashScreenManager.ShowForm(this, typeof(frmWaiting), true, true, false);
                frmImport frmList = new frmImport();
                xOpenForm(frmList);
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally
            {
                SplashScreenManager.CloseForm(false);
            }
        }

        private void mniImportNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                SplashScreenManager.ShowForm(this, typeof(frmWaiting), true, true, false);
                frmImportNew frmNewImport = new frmImportNew();
                xOpenForm(frmNewImport);
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally
            {
                SplashScreenManager.CloseForm(false);
            }
        }

        private void xOpenForm(Form frmOpen)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;

                //check
                foreach (Form form in Application.OpenForms)
                {
                    if (form.GetType() == frmOpen.GetType())
                    {
                        form.Activate();
                        return;
                    }
                }

                frmOpen.MdiParent = this;
                frmOpen.Show();

            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally
            {
                Cursor.Current = Cursors.Default;
                SplashScreenManager.CloseForm(false);
            }
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            try
            {
                Program.gobjDataDanhMuc = clsDbAccess.fncGetDanhMuc(Program.gobjDb);
                btnManageQty_ItemClick(null, null);
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void mniExportRetail_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                SplashScreenManager.ShowForm(this, typeof(frmWaiting), true, true, false);
                frmExportRetail frmNewExport = new frmExportRetail();
                xOpenForm(frmNewExport);
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally
            {
                SplashScreenManager.CloseForm(false);
            }
        }

        private void mniExportWholeSale_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                SplashScreenManager.ShowForm(this, typeof(frmWaiting), true, true, false);
                frmExportWholeSales frmNewExport = new frmExportWholeSales();
                xOpenForm(frmNewExport);
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally
            {
                SplashScreenManager.CloseForm(false);
            }
        }

        private void btnExportList_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                SplashScreenManager.ShowForm(this, typeof(frmWaiting), true, true, false);
                frmExport frmNewExport = new frmExport();
                xOpenForm(frmNewExport);
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally
            {
                SplashScreenManager.CloseForm(false);
            }
        }

        private void btnManageQty_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                SplashScreenManager.ShowForm(this, typeof(frmWaiting), true, true, false);
                frmManageProductQty frmQty = new frmManageProductQty();
                xOpenForm(frmQty);
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally
            {
                SplashScreenManager.CloseForm(false);
            }
        }

        private void mniExit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void mniAttribute_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                //SplashScreenManager.ShowForm(this, typeof(frmWaiting), true, true, false);

                frmAttribute frmAttr = new frmAttribute();
                frmAttr.ShowDialog();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally
            {
                //SplashScreenManager.CloseForm(false);
            }
        }

        private void mniAddProduct_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                //SplashScreenManager.ShowForm(this, typeof(frmWaiting), true, true, false);

                frmAddProduct frmAdd = new frmAddProduct();
                frmAdd.ShowDialog();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally
            {
                //SplashScreenManager.CloseForm(false);
            }
        }

        private void mniUpdateQty_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                clsDbAccess.fncUpdateSoLuongTuDb(Program.gobjDb, Program.gobjTrans);
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }
        
        private void mniBaoCaoSoLuong_ItemClick_1(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                frmReportQty frmReportQty = new frmReportQty();
                frmReportQty.ShowDialog();
                if (frmReportQty.ReportData == null) return;

                //xuat bao cao
                SplashScreenManager.ShowForm(this, typeof(frmWaiting), true, true, false);

                frmExcel frmRptQty = new frmExcel();
                frmRptQty.fncReportQty(frmReportQty.ReportData);
                xOpenForm(frmRptQty);
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally
            {
                SplashScreenManager.CloseForm(false);
            }
        }

        private void mniBaoCaoKhachHang_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                //xuat bao cao
                SplashScreenManager.ShowForm(this, typeof(frmWaiting), true, true, false);

                frmCustomerReport frmRpt = new frmCustomerReport();
                xOpenForm(frmRpt);
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally
            {
                SplashScreenManager.CloseForm(false);
            }
        }
    }
}